package cn.yq.aio.ui.main;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import cn.yq.aio.R;
import cn.yq.aio.base.MyApp;
import cn.yq.aio.data.MySp;
import cn.yq.aio.databinding.FragmentMainLeftBinding;

/**
 * @AUTHOR YangLu
 * @Time 2021-03-25
 * @todo 测量页左侧
 */
public class MainLeftFragment extends Fragment {

    public MainLeftFragment() {
        // Required empty public constructor
    }

    FragmentMainLeftBinding binding;
    OnFunctionClick onFunctionClick;

    public static MainLeftFragment newInstance() {
        MainLeftFragment fragment = new MainLeftFragment();
        return fragment;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof OnFunctionClick) {
            onFunctionClick = (OnFunctionClick) context;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
        onFunctionClick = null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMainLeftBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding.nameLeftMain.setText("韩梅梅");
        binding.phoneLeftMain.setText("手机：12312345678");
        SpannableString ss = new SpannableString(" " + " 查看报告");
        Drawable d = ContextCompat.getDrawable(getContext(), R.mipmap.look_left_main);
        d.setBounds(0, 0, MyApp.dp2px(8), MyApp.dp2px(8));
        ss.setSpan(new ImageSpan(d, ImageSpan.ALIGN_CENTER), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.lookLeftMain.setText(ss);

        binding.rgLeftMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (onFunctionClick == null) return;
                if (binding.rgShengaoLeftMain.isChecked()) {
                    onFunctionClick.clickShenGao();
                } else if (binding.rgXindianLeftMain.isChecked()) {
                    onFunctionClick.clickXinDian();
                } else if (binding.rgTiwenLeftMain.isChecked()) {
                    onFunctionClick.clickTiWen();
                } else if (binding.rgXueyaLeftMain.isChecked()) {
                    onFunctionClick.clickXueYa();
                } else if (binding.rgXuetangLeftMain.isChecked()) {
                    onFunctionClick.clickXueTang();
                } else if (binding.rgXueyangLeftMain.isChecked()) {
                    onFunctionClick.clickXueYang();
                } else if (binding.rgChengfenLeftMain.isChecked()) {
                    onFunctionClick.clickChengFen();
                }
            }
        });
    }

    public interface OnFunctionClick {
        void clickShenGao();

        void clickXinDian();

        void clickTiWen();

        void clickXueYa();

        void clickXueTang();

        void clickXueYang();

        void clickChengFen();
    }
}
